{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "title: Ownership and borrowing\n",
    "order: 3\n",
    "sidebar_position: 3\n",
    "description: How Mojo shares references through function arguments.\n",
    "css: /static/styles/page-navigation.css\n",
    "website:\n",
    "  open-graph:\n",
    "    image: /static/images/mojo-social-card.png\n",
    "  twitter-card:\n",
    "    image: /static/images/mojo-social-card.png\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A challenge you might face when using some programming languages is that you\n",
    "must manually allocate and deallocate memory. When multiple parts of the\n",
    "program need access to the same memory, it becomes difficult to keep track of\n",
    "who \"owns\" a value and determine when is the right time to deallocate it. If\n",
    "you make a mistake, it can result in a \"use-after-free\" error, a \"double free\"\n",
    "error, or a \"leaked memory\" error, any one of which can be catastrophic.\n",
    "\n",
    "Mojo helps avoid these errors by ensuring there is only one variable that owns\n",
    "each value at a time, while still allowing you to share references with other\n",
    "functions. When the lifetime of the owner ends, Mojo [destroys the\n",
    "value](/mojo/manual/lifecycle/death.html).\n",
    "\n",
    "On this page, we'll explain the rules that govern this ownership model and how\n",
    "to specify different argument conventions that define how values are shared into\n",
    "functions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Argument conventions\n",
    "\n",
    "In all programming languages, code quality and performance is heavily dependent\n",
    "upon how functions treat argument values. That is, whether a value received by\n",
    "a function is a unique value or a reference, and whether it's mutable or\n",
    "immutable, has a series of consequences that define the readability,\n",
    "performance, and safety of the language.\n",
    "\n",
    "In Mojo, we want to provide full [value\n",
    "semantics](/mojo/manual/values/value-semantics.html) by default, which provides\n",
    "consistent and predictable behavior. But as a systems programming language, we\n",
    "also need to offer full control over memory optimizations, which generally\n",
    "requires reference semantics. The trick is to introduce reference semantics in\n",
    "a way that ensures all code is memory safe by tracking the lifetime of every\n",
    "value and destroying each one at the right time (and only once). All of this is\n",
    "made possible in Mojo through the use of argument conventions that ensure every\n",
    "value has only one owner at a time.\n",
    "\n",
    "An argument convention specifies whether an argument is mutable or immutable,\n",
    "and whether the function owns the value. Each convention is defined by a\n",
    "keyword at the beginning of an argument declaration:\n",
    "\n",
    "- `borrowed`: The function receives an **immutable reference**. This means the\n",
    "  function can read the original value (it is *not* a copy), but it cannot\n",
    "  mutate (modify) it.\n",
    "  \n",
    "- `inout`: The function receives a **mutable reference**. This means the\n",
    "  function can read and mutate the original value (it is *not* a copy).\n",
    "  \n",
    "- `owned`: The function takes **ownership**. This means the function has\n",
    "  exclusive mutable access to the argument—the function caller does not have\n",
    "  access to this value (anymore). Often, this also implies that the caller\n",
    "  should transfer ownership to this function, but that's not always what\n",
    "  happens and this might instead be a copy (as you'll learn below).\n",
    "\n",
    "For example, this function has one argument that's a mutable\n",
    "reference and one that's immutable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "fn add(inout x: Int, borrowed y: Int):\n",
    "    x += y\n",
    "\n",
    "fn main():\n",
    "    var a = 1\n",
    "    var b = 2\n",
    "    add(a, b)\n",
    "    print(a)  # Prints 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You've probably already seen some function arguments that don't declare a\n",
    "convention. That's because every argument has a default convention, depending\n",
    "on whether the function is declared with `fn` or `def`:\n",
    "\n",
    "- All values passed into a Mojo [`def`\n",
    "  function](/mojo/manual/functions.html#def-functions) are `owned`,\n",
    "  by default. \n",
    "\n",
    "- All values passed into a Mojo [`fn`\n",
    "  function](/mojo/manual/functions.html#fn-functions) are `borrowed`,\n",
    "  by default.\n",
    "\n",
    "In the following sections, we'll explain each of these argument conventions in\n",
    "more detail."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ownership summary\n",
    "\n",
    "The fundamental rules that make Mojo's ownership model work are the following:\n",
    "\n",
    "- Every value has only one owner at a time.\n",
    "- When the lifetime of the owner ends, Mojo destroys the value.\n",
    "\n",
    "The \"borrow checker\" is a process in the Mojo compiler that ensures there is\n",
    "only one owner for each value at any time. It also enforces some other\n",
    "memory-safety rules:\n",
    "\n",
    "- You cannot create multiple mutable references (`inout`) for the same value.\n",
    "  (Multiple immutable references (`borrowed`) are okay.)\n",
    "\n",
    "- You cannot create a mutable reference (`inout`) if there exists an\n",
    "  immutable reference (`borrowed`) for the same value. (TODO: Not currently\n",
    "  implemented.)\n",
    "\n",
    "Because Mojo does not allow a mutable reference to overlap with another mutable\n",
    "or immutable reference, it provides a predictable programming model about which\n",
    "references are and aren't valid (an invalid reference is one who's lifetime has\n",
    "ended, perhaps because the value ownership was transferred). Importantly, this\n",
    "logic allows Mojo to immediately [destroy\n",
    "values](/mojo/manual/lifecycle/death.html) when their lifetime ends."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Immutable arguments (`borrowed`)\n",
    "\n",
    "If you'd like your function to receive an **immutable reference**, add the\n",
    "`borrowed` keyword in front of the argument name.\n",
    "\n",
    "The `borrowed` convention is the default for all arguments in an `fn` function,\n",
    "but you can still specify it to be explicit. It also works on `def` functions,\n",
    "which otherwise receive arguments by value, which might not be desirable, such\n",
    "as when the type is expensive to copy (or not copyable at all) and you just\n",
    "need to read it. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "256x256\n"
     ]
    }
   ],
   "source": [
    "from tensor import Tensor, TensorShape\n",
    "\n",
    "def print_shape(borrowed tensor: Tensor[DType.float32]):\n",
    "    shape = tensor.shape()\n",
    "    print(shape.__str__())\n",
    "\n",
    "var tensor = Tensor[DType.float32](256, 256)\n",
    "print_shape(tensor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In general, passing an immutable reference is much more efficient when handling\n",
    "large or expensive-to-copy values, because the copy constructor and destructor\n",
    "are not invoked for a borrow.\n",
    "\n",
    "### Compared to C++ and Rust\n",
    "\n",
    "Mojo's borrowed argument convention is similar in some ways to passing an\n",
    "argument by `const&` in C++, which also avoids a copy of the value and disables\n",
    "mutability in the callee. However, the borrowed convention differs from\n",
    "`const&` in C++ in two important ways:\n",
    "\n",
    "- The Mojo compiler implements a borrow checker (similar to Rust) that prevents\n",
    "code from dynamically forming mutable references to a value when there are\n",
    "immutable references outstanding, and it prevents multiple mutable references\n",
    "to the same value.\n",
    "\n",
    "- Small values like `Int`, `Float`, and `SIMD` are passed directly in machine\n",
    "registers instead of through an extra indirection (this is because they are\n",
    "declared with the `@register_passable` decorator). This is a [significant\n",
    "performance\n",
    "enhancement](https://www.forrestthewoods.com/blog/should-small-rust-structs-be-passed-by-copy-or-by-borrow/)\n",
    "when compared to languages like C++ and Rust, and moves this optimization from\n",
    "every call site to a declaration on the type definition.\n",
    "\n",
    "Similar to Rust, Mojo's borrow checker enforces the exclusivity of invariants.\n",
    "The major difference between Rust and Mojo is that Mojo does not require a\n",
    "sigil on the caller side to pass by borrow. Also, Mojo is more efficient when\n",
    "passing small values, and Rust defaults to moving values instead of passing\n",
    "them around by borrow. These policy and syntax decisions allow Mojo to provide\n",
    "an easier-to-use programming model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mutable arguments (`inout`)\n",
    "\n",
    "If you'd like your function to receive a **mutable reference**, add the `inout`\n",
    "keyword in front of the argument name. You can think of `inout` like this: it\n",
    "means any changes to the value *in*side the function are visible *out*side the\n",
    "function.\n",
    "\n",
    "For example, this `mutate()` function updates the original `x` value:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "def mutate(inout y: Int):\n",
    "    y += 1\n",
    "\n",
    "var x = 1\n",
    "mutate(x)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That behaves like an optimized shorthand for this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "def mutate_copy(y: Int) -> Int:\n",
    "    y += 1\n",
    "    return y\n",
    "\n",
    "var x = 1\n",
    "x = mutate_copy(x)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Although the code using `inout` isn't that much shorter, it's more memory\n",
    "efficient because it does not make a copy of the value.\n",
    "\n",
    "However, remember that the values passed as `inout` must already be mutable.\n",
    "For example, if you try to take a `borrowed` value and pass it to another\n",
    "function as `inout`, you'll get a compiler error because Mojo can't form a\n",
    "mutable reference from an immutable reference.\n",
    "\n",
    ":::note\n",
    "\n",
    "Notice that we don't call this argument passing \"by reference.\"\n",
    "Although the `inout` convention is conceptually the same, we don't call it\n",
    "by-reference passing because the implementation may actually pass values using\n",
    "pointers.\n",
    "\n",
    ":::\n",
    "\n",
    ":::note\n",
    "\n",
    "You cannot define [default\n",
    "values](/mojo/manual/functions.html#optional-arguments) for `inout`\n",
    "arguments.\n",
    "\n",
    ":::"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Transfer arguments (`owned` and `^`)\n",
    "\n",
    "And finally, if you'd like your function to receive value **ownership**, add the\n",
    "`owned` keyword in front of the argument name.\n",
    "\n",
    "This convention is usually combined with use of the postfixed `^` \"transfer\"\n",
    "operator on the variable that is passed into the function, which ends the\n",
    "lifetime of that variable.\n",
    "\n",
    "Technically, the `owned` keyword does not guarantee that the received value is\n",
    "a mutable reference to _the original value_—it guarantees only that the function\n",
    "gets unique ownership of this particular value (enforcing [value\n",
    "semantics](/mojo/manual/values/value-semantics.html)). This happens in one of\n",
    "two ways:\n",
    "\n",
    "- The caller passes the argument with the `^` transfer operator, which ends the\n",
    "lifetime of that variable (the variable becomes invalid) and ownership is\n",
    "transferred into the function without making a copy of any heap-allocated data.\n",
    "\n",
    "- The caller **does not** use the `^` transfer operator, in which case, the\n",
    "value is copied into the function argument and the original variable remains\n",
    "valid (unless it is not used again, in which case the compiler destroys the\n",
    "variable anyway because its lifetime naturally ends there).\n",
    "\n",
    "Regardless, when the function declares an argument as `owned`, it can be certain\n",
    "that it has unique mutable access to that value. \n",
    "\n",
    "For example, the following code works by making a copy of the string,\n",
    "because—although `take_text()` uses the `owned` convention—the caller does not\n",
    "include the transfer operator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello!\n",
      "Hello\n"
     ]
    }
   ],
   "source": [
    "fn take_text(owned text: String):\n",
    "    text += \"!\"\n",
    "    print(text)\n",
    "\n",
    "fn my_function():\n",
    "    var message: String = \"Hello\"\n",
    "    take_text(message)\n",
    "    print(message)\n",
    "\n",
    "my_function()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, if you add the `^` transfer operator when calling `take_text()`, the\n",
    "compiler complains about `print(message)`, because at that point, the `message`\n",
    "variable is no longer initialized. That is, this version does not compile:\n",
    "\n",
    "```mojo\n",
    "fn my_function():\n",
    "    var message: String = \"Hello\"\n",
    "    take_text(message^)  \n",
    "    print(message)  # ERROR: The `message` variable is uninitialized\n",
    "```\n",
    "\n",
    "This is a critical feature of Mojo's borrow checker, because it ensures that no\n",
    "two variables can have ownership of the same value. To fix the error, you must\n",
    "not use the `message` variable after you end its lifetime with the `^` transfer\n",
    "operator. So here is the corrected code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello!\n"
     ]
    }
   ],
   "source": [
    "\n",
    "fn my_function():\n",
    "    var message: String = \"Hello\"\n",
    "    take_text(message^)\n",
    "\n",
    "my_function()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ":::note\n",
    "\n",
    "Value lifetimes are not fully implemented for top-level code in\n",
    "Mojo's REPL, so the transfer operator currently works as intended only when\n",
    "used inside a function.\n",
    "\n",
    ":::"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transfer implementation details\n",
    "\n",
    "In Mojo, it's important that you not conflate \"ownership transfer\" with a \"move\n",
    "operation\"—these are not strictly the same thing. \n",
    "\n",
    "There are multiple ways that Mojo can transfer ownership of a value without\n",
    "making a copy:\n",
    "\n",
    "- If a type implements the [move\n",
    "  constructor](/mojo/manual/lifecycle/life.html#consuming-move-constructor),\n",
    "  `__moveinit__()`, Mojo may invoke this method _if_ a value of that type is\n",
    "  transferred into a function as an `owned` argument, _and_ the original value's\n",
    "  lifetime ends at the same point (with or without use of the `^` transfer\n",
    "  operator).\n",
    "\n",
    "- If a type hasn't implemented `__moveinit__()` Mojo may transfer ownership by\n",
    "  simply passing the recipient a reference to the value in the caller's stack.\n",
    "\n",
    "In order for the `owned` convention to work _without_ the transfer\n",
    "operator, the value type must be copyable (via `__copyinit__()`)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparing `def` and `fn` argument conventions\n",
    "\n",
    "As mentioned in the section about\n",
    "[functions](/mojo/manual/functions.html), the `def` and `fn` functions\n",
    "are interchangeable, as far as a caller is concerned, and they can both\n",
    "accomplish the same things. It's only the inside that differs, and Mojo's `def`\n",
    "function is essentially just sugaring for the `fn` function:\n",
    "\n",
    "- A `def` argument without a type annotation defaults to\n",
    "  [`object`](/mojo/stdlib/builtin/object#object) type (whereas as `fn`\n",
    "  requires all types be explicitly declared).\n",
    "\n",
    "- A `def` argument without a convention keyword (`borrowed`, `inout`, or\n",
    "  `owned`) defaults to `owned` (it receives a copy with a mutable variable).\n",
    "\n",
    "For example, these two functions have the exact same behavior:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def example(borrowed a: Int, inout b: Int, c):\n",
    "    pass\n",
    "\n",
    "fn example(a: Int, inout b: Int, owned c: object):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or, instead of specifying `owned` for the `c` argument, you can get the same\n",
    "effect by manually making a copy when you need it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fn example(a: Int, inout b: Int, c_in: object):\n",
    "    var c = c_in\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This shadow copy typically adds no overhead, because references for small types\n",
    "like `object` are cheap to copy. The expensive part is adjusting the reference\n",
    "count, but that's also eliminated by a compiler optimization."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Mojo",
   "language": "mojo",
   "name": "mojo-jupyter-kernel"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "mojo"
   },
   "file_extension": ".mojo",
   "mimetype": "text/x-mojo",
   "name": "mojo"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
